﻿<?xml version="1.0" encoding="utf-8"?><Articles><Article><ArticlesID>882</ArticlesID><Title>H&amp;#224;m chuyển đổi từ Số sang chữ</Title><Body>&amp;lt;p&amp;gt;Code VB.NET&amp;lt;/p&amp;gt;
&amp;lt;pre class=&amp;quot;brush: vb;&amp;quot; title=&amp;quot;code&amp;quot;&amp;gt;
    Public Function NumToString(ByVal nNumber As Currency)  As String
     
    Dim bNegative As Boolean
    Dim bHundred As Boolean
     
    If nNumber &amp;amp;lt; 0 Then
        bNegative = True
    End If
     
   nNumber = Abs(Int(nNumber))
    
   If nNumber &amp;amp;lt; 1000 Then
       If nNumber \ 100 &amp;amp;gt; 0 Then
           NumToString = NumToString &amp;amp;amp; _
                NumToString(nNumber \ 100) &amp;amp;amp; &amp;amp;quot; hundred&amp;amp;quot;
           bHundred = True
       End If
       nNumber = nNumber - ((nNumber \ 100) * 100)
       Dim bNoFirstDigit As Boolean
       bNoFirstDigit = False
       Select Case nNumber \ 10
           Case 0
               Select Case nNumber Mod 10
                   Case 0
                       If Not bHundred Then
                           NumToString = NumToString &amp;amp;amp; &amp;amp;quot; zero&amp;amp;quot;
                       End If
                   Case 1: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; one&amp;amp;quot;
                   Case 2: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; two&amp;amp;quot;
                   Case 3: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; three&amp;amp;quot;
                   Case 4: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; four&amp;amp;quot;
                   Case 5: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; five&amp;amp;quot;
                   Case 6: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; six&amp;amp;quot;
                   Case 7: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; seven&amp;amp;quot;
                   Case 8: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; eight&amp;amp;quot;
                   Case 9: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; nine&amp;amp;quot;
               End Select
               bNoFirstDigit = True
           Case 1
               Select Case nNumber Mod 10
                   Case 0: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; ten&amp;amp;quot;
                   Case 1: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; eleven&amp;amp;quot;
                   Case 2: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; twelve&amp;amp;quot;
                   Case 3: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; thirteen&amp;amp;quot;
                   Case 4: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; fourteen&amp;amp;quot;
                   Case 5: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; fifteen&amp;amp;quot;
                   Case 6: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; sixteen&amp;amp;quot;
                   Case 7: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; seventeen&amp;amp;quot;
                   Case 8: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; eighteen&amp;amp;quot;
                   Case 9: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; nineteen&amp;amp;quot;
               End Select
               bNoFirstDigit = True
           Case 2: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; twenty&amp;amp;quot;
           Case 3: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; thirty&amp;amp;quot;
           Case 4: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; forty&amp;amp;quot;
           Case 5: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; fifty&amp;amp;quot;
           Case 6: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; sixty&amp;amp;quot;
           Case 7: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; seventy&amp;amp;quot;
           Case 8: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; eighty&amp;amp;quot;
           Case 9: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; ninety&amp;amp;quot;
       End Select
       If Not bNoFirstDigit Then
           If nNumber Mod 10 &amp;amp;lt;&amp;amp;gt; 0 Then
               NumToString = NumToString &amp;amp;amp; &amp;amp;quot;-&amp;amp;quot; &amp;amp;amp; _
                             Mid(NumToString(nNumber Mod 10), 2)
           End If
       End If
   Else
       Dim nTemp As Currency
       nTemp = 10 ^ 12 'trillion
       Do While nTemp &amp;amp;gt;= 1
           If nNumber &amp;amp;gt;= nTemp Then
               NumToString = NumToString &amp;amp;amp; _
                             NumToString(Int(nNumber / nTemp))
               Select Case Int(Log(nTemp) / Log(10) + 0.5)
                   Case 12: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; trillion&amp;amp;quot;
                   Case 9: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; billion&amp;amp;quot;
                   Case 6: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; million&amp;amp;quot;
                   Case 3: NumToString = NumToString &amp;amp;amp; &amp;amp;quot; thousand&amp;amp;quot;
               End Select
              
               nNumber = nNumber - (Int(nNumber / nTemp) * nTemp)
           End If
           nTemp = nTemp / 1000
       Loop
   End If
    
   If bNegative Then
       NumToString = &amp;amp;quot; negative&amp;amp;quot; &amp;amp;amp; NumToString
   End If
      
   End Function&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;&amp;amp;nbsp;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;Code C#&amp;lt;/p&amp;gt;
&amp;lt;pre class=&amp;quot;brush: csharp;&amp;quot; title=&amp;quot;code&amp;quot;&amp;gt;
public string NumToString(Currency nNumber)
{

	bool bNegative;
	bool bHundred;

	if (nNumber &amp;amp;lt; 0) {
		bNegative = true;
	}

	nNumber = Abs(Int(nNumber));

	if (nNumber &amp;amp;lt; 1000) {
		if (nNumber / 100 &amp;amp;gt; 0) {
			NumToString = NumToString + NumToString(nNumber / 100) + &amp;amp;quot; hundred&amp;amp;quot;;
			bHundred = true;
		}
		nNumber = nNumber - ((nNumber / 100) * 100);
		bool bNoFirstDigit;
		bNoFirstDigit = false;
		switch (nNumber / 10) {
			case 0:
				switch (nNumber % 10) {
					case 0:
						if (!bHundred) {
							NumToString = NumToString + &amp;amp;quot; zero&amp;amp;quot;;
						}
						break;
					case 1:
						NumToString = NumToString + &amp;amp;quot; one&amp;amp;quot;;
						break;
					case 2:
						NumToString = NumToString + &amp;amp;quot; two&amp;amp;quot;;
						break;
					case 3:
						NumToString = NumToString + &amp;amp;quot; three&amp;amp;quot;;
						break;
					case 4:
						NumToString = NumToString + &amp;amp;quot; four&amp;amp;quot;;
						break;
					case 5:
						NumToString = NumToString + &amp;amp;quot; five&amp;amp;quot;;
						break;
					case 6:
						NumToString = NumToString + &amp;amp;quot; six&amp;amp;quot;;
						break;
					case 7:
						NumToString = NumToString + &amp;amp;quot; seven&amp;amp;quot;;
						break;
					case 8:
						NumToString = NumToString + &amp;amp;quot; eight&amp;amp;quot;;
						break;
					case 9:
						NumToString = NumToString + &amp;amp;quot; nine&amp;amp;quot;;
						break;
				}
				bNoFirstDigit = true;
				break;
			case 1:
				switch (nNumber % 10) {
					case 0:
						NumToString = NumToString + &amp;amp;quot; ten&amp;amp;quot;;
						break;
					case 1:
						NumToString = NumToString + &amp;amp;quot; eleven&amp;amp;quot;;
						break;
					case 2:
						NumToString = NumToString + &amp;amp;quot; twelve&amp;amp;quot;;
						break;
					case 3:
						NumToString = NumToString + &amp;amp;quot; thirteen&amp;amp;quot;;
						break;
					case 4:
						NumToString = NumToString + &amp;amp;quot; fourteen&amp;amp;quot;;
						break;
					case 5:
						NumToString = NumToString + &amp;amp;quot; fifteen&amp;amp;quot;;
						break;
					case 6:
						NumToString = NumToString + &amp;amp;quot; sixteen&amp;amp;quot;;
						break;
					case 7:
						NumToString = NumToString + &amp;amp;quot; seventeen&amp;amp;quot;;
						break;
					case 8:
						NumToString = NumToString + &amp;amp;quot; eighteen&amp;amp;quot;;
						break;
					case 9:
						NumToString = NumToString + &amp;amp;quot; nineteen&amp;amp;quot;;
						break;
				}
				bNoFirstDigit = true;
				break;
			case 2:
				NumToString = NumToString + &amp;amp;quot; twenty&amp;amp;quot;;
				break;
			case 3:
				NumToString = NumToString + &amp;amp;quot; thirty&amp;amp;quot;;
				break;
			case 4:
				NumToString = NumToString + &amp;amp;quot; forty&amp;amp;quot;;
				break;
			case 5:
				NumToString = NumToString + &amp;amp;quot; fifty&amp;amp;quot;;
				break;
			case 6:
				NumToString = NumToString + &amp;amp;quot; sixty&amp;amp;quot;;
				break;
			case 7:
				NumToString = NumToString + &amp;amp;quot; seventy&amp;amp;quot;;
				break;
			case 8:
				NumToString = NumToString + &amp;amp;quot; eighty&amp;amp;quot;;
				break;
			case 9:
				NumToString = NumToString + &amp;amp;quot; ninety&amp;amp;quot;;
				break;
		}
		if (!bNoFirstDigit) {
			if (nNumber % 10 != 0) {
				NumToString = NumToString + &amp;amp;quot;-&amp;amp;quot; + Mid(NumToString(nNumber % 10), 2);
			}
		}
	} else {
		Currency nTemp;
		nTemp = Math.Pow(10, 12);
		//trillion
		while (nTemp &amp;amp;gt;= 1) {
			if (nNumber &amp;amp;gt;= nTemp) {
				NumToString = NumToString + NumToString(Int(nNumber / nTemp));
				switch (Int(Log(nTemp) / Log(10) + 0.5)) {
					case 12:
						NumToString = NumToString + &amp;amp;quot; trillion&amp;amp;quot;;
						break;
					case 9:
						NumToString = NumToString + &amp;amp;quot; billion&amp;amp;quot;;
						break;
					case 6:
						NumToString = NumToString + &amp;amp;quot; million&amp;amp;quot;;
						break;
					case 3:
						NumToString = NumToString + &amp;amp;quot; thousand&amp;amp;quot;;
						break;
				}

				nNumber = nNumber - (Int(nNumber / nTemp) * nTemp);
			}
			nTemp = nTemp / 1000;
		}
	}

	if (bNegative) {
		NumToString = &amp;amp;quot; negative&amp;amp;quot; + NumToString;
	}

}
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Sưu tầm&amp;lt;/p&amp;gt;</Body><CommentEnabled>True</CommentEnabled><ViewCount>0</ViewCount><ReleaseDate>4/20/2010 7:01:13 AM</ReleaseDate><TotalComment>0</TotalComment><IsMedia>False</IsMedia><IsPicture>False</IsPicture><IsVote>True</IsVote><ArticlePassword /><PostVisible>0</PostVisible><ArticlePath>/2010/4/10/Ham-chuyen-doi-tu-So-sang-chu-372</ArticlePath><Approved>True</Approved><ImagePath /><CategoryId>28</CategoryId><ArrayCatID>28|25</ArrayCatID></Article></Articles>